#include "approvedialog.h"
#include "confirmdialog.h"
#include "treedoc.h"

ApproveDialog::ApproveDialog(Action *action, QWidget *parent) :
    QDialog(parent),m_action(action),m_tab(parent),
    m_content(parent), m_opinion(parent), m_contentChange(parent), m_expiryChange(parent),
    m_okButton(tr("Готово"),this), m_cancelButton(tr("Отмена"),this)
{
    QVBoxLayout *main = new QVBoxLayout;
    main->addWidget(&m_tab);
    QHBoxLayout *hlay = new QHBoxLayout;
    hlay->addStretch();
    hlay->addWidget(&m_okButton);
    connect(&m_okButton,SIGNAL(clicked()),this,SLOT(onOkButtonClick()));
    connect(&m_cancelButton,SIGNAL(clicked()),this,SLOT(reject()));
    hlay->addWidget(&m_cancelButton);
    main->addLayout(hlay);
    m_tab.setTabPosition(QTabWidget::West);
    setLayout(main);
    createTabs();
    setFixedHeight(sizeHint().height());
    setFixedWidth(sizeHint().width()+40);
    setModal(true);
    setWindowTitle(tr("Согласование"));
    setWindowIcon(QIcon(":/approve"));
    connect(&m_contentChange,SIGNAL(toggled(bool)),&m_content,SLOT(setEnabled(bool)));
    connect(&m_expiryChange,SIGNAL(toggled(bool)),&m_expiry,SLOT(setEnabled(bool)));
}
void ApproveDialog::onOkButtonClick()
{
    ConfirmDialog dialog(dynamic_cast<TreeDoc*>(m_action->treeWidget())->db(),this);
    dialog.exec();
    if(dialog.result()==QDialog::Accepted)
    {
        switch(m_tab.currentIndex())
        {
        case 0:{
                dynamic_cast<TreeDoc*>(m_action->treeWidget())->
                        execute(tr("select =schema=.ex_agreement(%1,true)").arg(m_action->id()));
                break;
            }
        case 1:{
                dynamic_cast<TreeDoc*>(m_action->treeWidget())->
                        execute(tr("select =schema=.ex_agreement(%1,%2,%3)")
                                .arg(m_action->id())
                                .arg(m_contentChange.isChecked()?tr("'%1'").arg(m_content.document()->toPlainText()):"NULL")
                                .arg(m_expiryChange.isChecked()?tr("'%1'").arg(m_expiry.date().toString("yyyy-MM-dd")):"NULL"));
                break;
            }
        case 2:{
                dynamic_cast<TreeDoc*>(m_action->treeWidget())->
                        execute(tr("select =schema=.ex_agreement(%1,'%2')")
                                .arg(m_action->id())
                                .arg(m_opinion.document()->toPlainText()));
                break;
            }
        }
    }
    accept();
}
void ApproveDialog::createTabs()
{
    QWidget* widget = new QWidget(this);
    QVBoxLayout *main = new QVBoxLayout;
    main->setAlignment(Qt::AlignCenter);
    main->addStretch();
    main->addWidget(new QLabel(tr("<h3>С содержанием мероприятия и датой<br>"
                                  "выполнения мероприятия согласен."),widget));
    main->addStretch();
    widget->setLayout(main);
    m_tab.addTab(widget,tr("Согласовать"));
    m_tab.setTabEnabled(0,m_action->operations()->isEnabled(13));

    widget = new QWidget(this);
    main = new QVBoxLayout;
    main->setAlignment(Qt::AlignCenter);
    main->addStretch();
    main->addWidget(&m_contentChange);
    m_contentChange.setText(tr("Предлагаю другую форулировку:"));
    m_contentChange.setChecked(true);
    main->addWidget(&m_content);
    m_expiryChange.setText(tr("Прошу изменить дату выполнения:"));
    main->addWidget(&m_expiryChange);
    main->addWidget(&m_expiry);
    m_expiry.setEnabled(false);
    m_expiry.setDate(m_action->expiry());
    m_content.setText(m_action->content());
    main->addStretch();
    widget->setLayout(main);
    m_tab.addTab(widget,tr("Изменить"));
    m_tab.setTabEnabled(1,m_action->operations()->isEnabled(14));

    widget = new QWidget(this);
    main = new QVBoxLayout;
    main->setAlignment(Qt::AlignCenter);
    main->addWidget(new QLabel(tr("Особое мнение:"),widget));
    main->addWidget(&m_opinion);
    main->addStretch();
    widget->setLayout(main);
    m_tab.addTab(widget,tr("Согласовать с ОМ"));
    m_tab.setTabEnabled(2,m_action->operations()->isEnabled(15));
    m_okButton.setEnabled(m_tab.isTabEnabled(0)||m_tab.isTabEnabled(1)||m_tab.isTabEnabled(2));
}
